package com.stock.demo.function.start.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.stock.demo.MainActivity;
import com.stock.demo.R;
import com.stock.demo.framework.ui.TipType;
import com.stock.demo.framework.ui.activity.BaseActivity;
import com.stock.demo.framework.ui.view.EditTextWithDelBtn;
import com.stock.demo.function.start.presenter.RegisterPresenter;
import com.stock.demo.function.start.presenter.RegisterPresenterImpl;
import com.stock.demo.util.CommonUtils;

public class RegisterActivity extends BaseActivity implements RegisterView, EditTextWithDelBtn.OnContentChangeListener {

    private ImageView mIvPhone;
    private EditTextWithDelBtn mEtPhone;
    private EditTextWithDelBtn mEtSms;
    private ImageView mIvSms;
    private ImageView mIvPassword;
    private EditTextWithDelBtn mEtPassword;
    private TextView mTvGetCode;
    private Button mBtnRegister;
    private TextView mTvAgreement;

    private RegisterPresenter mPresenter;
    private int mSeconds = 60;

    public static void startAction(Context context) {
        Intent intent = new Intent(context, RegisterActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initData();
        initListener();
    }

    private void initView() {
        mIvPhone = (ImageView) findViewById(R.id.iv_register_phone);
        mEtPhone = (EditTextWithDelBtn) findViewById(R.id.et_register_phone);
        mEtSms = (EditTextWithDelBtn) findViewById(R.id.et_register_sms);
        mIvSms = (ImageView) findViewById(R.id.iv_register_sms);
        mIvPassword = (ImageView) findViewById(R.id.iv_register_pw);
        mTvGetCode = (TextView) findViewById(R.id.tv_register_get_code);
        mEtPassword = (EditTextWithDelBtn) findViewById(R.id.et_register_pw);
        mBtnRegister = (Button) findViewById(R.id.btn_register_and_login);
        mTvAgreement = (TextView) findViewById(R.id.tv_register_agreement);
    }

    private void initData() {
        mPresenter = new RegisterPresenterImpl(this);
    }

    private void initListener() {
        mTvGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (CommonUtils.checkPhone(mEtPhone.getText())) {
                    mPresenter.getCode(mEtPhone.getText());
                } else {
                    showTip(TipType.ERROR, "请输入有效手机号码");
                }
            }
        });
        mBtnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkRegisterData()) {
                    mPresenter.register(mEtPhone.getText(), mEtPassword.getText());
                }
            }
        });
        mEtPhone.setOnContentChangeListener(this);
        mEtSms.setOnContentChangeListener(this);
        mEtPassword.setOnContentChangeListener(this);
    }

    private boolean checkRegisterData() {
        return CommonUtils.checkPhone(mEtPhone.getText()) && TextUtils.equals("888888", mEtSms.getText()) && (!TextUtils.isEmpty(mEtPassword.getText()));
    }

    @Override
    public void onGetCode(boolean isSuccess) {
        if (isSuccess) {
            showTip(TipType.ERROR, "Demo的验证码为888888");
            initTime();
        }
    }

    @Override
    public void onRegister(boolean isSuccess) {
        if (isSuccess) {
            showTip(TipType.SUCCESS, "用户注册成功");
            mPresenter.login(mEtPhone.getText(), mEtPassword.getText());
        }
    }

    @Override
    public void onLogin(boolean isSuccess) {
        if (isSuccess) {
            MainActivity.startAction(this);
            finish();
        }
    }

    @Override
    public void beforeContentChange(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void OnContentChange(CharSequence s, int start, int before, int count) {
        if (mEtPhone.getText().length() == 0) {
            mIvPhone.setImageResource(R.mipmap.login_icon_user);
        } else {
            mIvPhone.setImageResource(R.mipmap.login_icon_user_focused);
        }
        if (mEtSms.getText().length() == 0) {
            mIvSms.setImageResource(R.mipmap.register_icon_lock);
        } else {
            mIvSms.setImageResource(R.mipmap.register_icon_lock_focused);
        }
        if (mEtPassword.getText().length() == 0) {
            mIvPassword.setImageResource(R.mipmap.login_icon_password);
        } else {
            mIvPassword.setImageResource(R.mipmap.login_icon_password_focused);
        }
        if (mEtPhone.getText().length() > 0 && mEtSms.getText().length() > 0 && mEtPassword.getText().length() > 0) {
            mBtnRegister.setTextColor(getResources().getColor(R.color.colorWhite));
            mBtnRegister.setClickable(true);
        } else {
            mBtnRegister.setTextColor(getResources().getColor(R.color.colorBtnHint));
            mBtnRegister.setClickable(false);
        }
    }

    @Override
    public void afterContentChange(Editable s) {

    }

    private void initTime() {
        mTvGetCode.postDelayed(new Runnable() {
            @Override
            public void run() {
                mSeconds--;
                mTvGetCode.setText("重新发送（" + mSeconds + "秒）");
                if (mSeconds > 0) {
                    initTime();
                } else {
                    mTvGetCode.setText("重发验证码");
                    mSeconds = 60;
                    mTvGetCode.setEnabled(true);
                }
            }
        }, 1000);
    }

}
